var app = getApp();

Page({
  data: {
    star: 0,
    starMap: [
      { id: '1', value: '1星' },
      { id: '2', value: '2星' },
      { id: '3', value: '3星' },
      { id: '4', value: '4星' },
      { id: '5', value: '5星' }
    ],
    comment: '',
    commentList: [],
    type_text: ''
  }, 
  onLoad: function (options) {
    var that = this;
    that.setData({
      orderId: options.orderId
    })
  },
  starChoose(e) {
    var that = this;
    let star = parseInt(e.target.dataset.star) || 0;
    that.setData({
      star: star,
    });
    that.getData();
  },
  getData: function () {
    var that = this;
    app.JsonPost("Api/OrderReview/getCommentOption", {}, function (res) {
      var comment = "review_" + that.data.star;
      var comments = [];

      for (var i = 0; i < res[comment].length; i++) {
        comments.push({
          text: res[comment][i],
          checked: false
        });
      }
      console.log(comments);
      that.setData({
        comment: comment,
        commentList: comments
      });

    });
  },
  commentSelect: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.commentList[index].checked = !that.data.commentList[index].checked;
    that.setData({
      commentList: that.data.commentList
    })
  },
  sendtap: function () {
    var that = this;

    //获取评语
    var commentList = that.data.commentList;
    var type_text = '';
    for (var i in commentList) {
      if (commentList[i].checked == true) {
        type_text += commentList[i].text + "|";
      }
    }
    that.setData({
      type_text: type_text.substring(0, type_text.length - 1)
    });

    //判断是否评星
    if (that.data.comment == '') {
      wx.showModal({
        title: '提示',
        content: '您还没有评星!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    //判断是否选择评语
    if (that.data.type_text == '') {
      wx.showModal({
        title: '提示',
        content: '您还没有选择评语!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    //评价
    var jsonData = {
      order_id: that.data.orderId,
      type: that.data.comment,
      type_text: that.data.type_text
    };
    app.showLoading("正在提交...请稍后...");
    app.JsonPost("Api/OrderReview/add", jsonData, function (data) {
      app.closeLoading();
      if (data !== false) {
        wx.showModal({
          title: '提示',
          content: '评价成功！',
          showCancel: false,
          success: function (res) {
            wx.navigateBack({
              delta: 1
            });
          }
        });
      }
    })
  }
})